package com.erp.Struts.Action;

import java.util.Date;
import com.erp.Struts.common.Constants;
import com.erp.hibernate.beans.Employee;
import com.erp.hibernate.beans.Inbill;
import com.erp.hibernate.dao.InbillDAO;

public class InBillAction extends PageAction {

	private static final long serialVersionUID = -6636530642458335666L;
	private InbillDAO inbillDAO;
	private Inbill inbill;
	
	public String init() throws Exception{
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getInbillDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	public String list() throws Exception{
		super.pager = this.getInbillDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	public String add() throws Exception{
		reset();
		return Constants.ADD_KEY;
	}
	
	public String insert() throws Exception{
		inbill.setDate(new Date());
		inbill.setEmployee((Employee)getSession(Constants.EMPLOYEE_KEY));
		this.getInbillDAO().save(inbill);
		inbill.setDate(new Date());
		inbill.setEmployee((Employee)getSession(Constants.EMPLOYEE_KEY));
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getInbillDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	public String edit() throws Exception{
		if (this.getInbill().getId() == null) {
			return Constants.LIST_KEY;
		} else {
			Inbill bill2 = this.getInbillDAO().findById(this.getInbill().getId());

			if (bill2 == null) {
				return Constants.LIST_KEY;
			} else {
				inbill = bill2;
				return Constants.EDIT_KEY;
			}
		}
	}
	
	public String saveedit() throws Exception{
		this.getInbillDAO().update(inbill);

		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getInbillDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	private void reset() {
		inbill = null;
	}

	public InbillDAO getInbillDAO() {
		return inbillDAO;
	}

	public void setInbillDAO(InbillDAO inbillDAO) {
		this.inbillDAO = inbillDAO;
	}

	public Inbill getInbill() {
		return inbill;
	}

	public void setInbill(Inbill inbill) {
		this.inbill = inbill;
	}
}
